package extensionTools.smvCreator;
import java.util.ArrayList;
import java.util.Iterator;

/**
* This class represent an object in OPM
* @author Oshrit & Lital
*/
public class SmvObject 
{
         
         private long id;
         private String name;
         private boolean isEnvironmental;
         private ArrayList<State> stateList;
         private ArrayList<Link> linkList;
         
        /**
         * Class constructor
         * @param id
         * @param name
         * @param isEnvironmental
         */
         public SmvObject(long id, String name, boolean isEnvironmental) 
         {
                this.id = id;
                this.name = SmvOp.setFirstCharToLowerCase(name.replaceAll(" ", "_").replaceAll("\n", "_"));
                this.isEnvironmental = isEnvironmental;
                stateList = new ArrayList<State>();
                linkList = new ArrayList<Link>();
         }

        public void addState (State currState)
        {
                this.stateList.add(currState);
        }
                 
        public void setId(long Id) {
                id = Id;
        }
        public long getId() {
                return id;
        }
        
        public void setEnvironmental(boolean isEnvironmental) {
                this.isEnvironmental = isEnvironmental;
        }
        public boolean isEnvironmental() {
                return isEnvironmental;
        } 
        
        public void setStateList(ArrayList<State> stateList) {
                this.stateList = stateList;
        }
        public ArrayList<State> getStateList() {
                return stateList;
        }
        
        public void setLinkList(ArrayList<Link> linkList) {
                this.linkList = linkList;
        }
        public ArrayList<Link> getLinkList() {
                return linkList;
        }

        public void setName(String name) {
                this.name = SmvOp.setFirstCharToLowerCase(name.replaceAll(" ", "_").replaceAll("\n", "_"));
        }

        public String getName() {
                return name;
        }
        
        /**
         * This function checks if a certain link exists
         * @param link
         * @return true if such link exists, false otherwise
         */
        public boolean isLinkExist(Link link)
        {
                Iterator<Link> i = linkList.iterator();
                while(i.hasNext())
                {
                        Link currLink = (Link)i.next();
                        if(currLink.getSrcId() == link.getSrcId() &&
                           currLink.getDstId() == link.getDstId() &&    
                           currLink.getSrcType() == link.getSrcType() &&
                           currLink.getDstType() == link.getDstType() &&
                           currLink.getLinkType() == link.getLinkType())
                        {
                                return true; 
                        }
                } 
                return false;
        }
        
        /**
         * This function finds and returns the initial state, or null
         * if no such state exists
         * @return the initial state or null if no such state exists
         */
        public State getInitialState()
        {
                for (State state : stateList) 
                {
                        if (state.getType() == State.stateType.sInitial) 
                        {
                                return state;
                        }
                }
                // no such state exists
                return null;
        }
        
        /**
         * This function gets an Id, returns the object's state if it's belongs
         * to, the sState if it's the object's id or null if no such id exists.
         * @param dst
         * @return the State it belongs to, or null otherwise
         */
        public State idBelongsToObj(long dst) 
        {
                for (State state : stateList) 
                {
                        if(state.getId() == dst)
                        {
                                return state;
                        }
                }
                return null;
        }

}
